/**
 * 用户
 * */


var levelAry = [];


//选项
function getOption(v){
    var formData = {
    };
    $.ajax({
        type: apis.userOption.method,
        url: apis.userOption.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            levelAry = res.data.level;
            var h = '', i;
            h = '';
            h += '<option value="">-选择会员等级-</option>';
            for  (i = 0; i < levelAry.length; i ++) {
                if (v == levelAry[i].id) {
                    h += '<option value="' + levelAry[i].id + '" selected>';
                } else {
                    h += '<option value="' + levelAry[i].id + '">';
                }
                h += levelAry[i].title;
                h += '</option>';
            }
            $('#ulevel').html(h);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//获取列表
function getList(e){
    console.log(e);
    var formData = {
        start: pages.start,
        limit: pages.limit,
        //phone: e.phone,
        //nick: e.nick,
        //level: $('#ulevel').val(),
        phone: phone,
        nick: nick,
        serial: serial,
        id: id,
        level: ulevel,
    };
    $.ajax({
        type: apis.userIndex.method,
        url: apis.userIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            indexHtml(d);
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}
//列表字符串
function indexHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].serial + '</td>';
        h += '<td>' + d[i].phone + '</td>';
        h += '<td>' + d[i].nickname + '</td>';
        h += '<td>';
        if (d[i].avatarUrl != '') {
            h += '<img src="' + d[i].prefix + d[i].avatarUrl + '" height="50"/>';
        } else {
            h += '-';
        }
        h += '</td>';
        h += '<td>' + d[i].balance + '</td>';
        h += '<td>' + d[i].levelName + '</td>';
        h += '<td><a href="utrace.html?uid=' + d[i].id + '" style="color: #0088cc;">' + d[i].readNum + '</a>';
        h += '<td><a href="uaddress.html?uid=' + d[i].id + '" style="color: #0088cc;">' + d[i].addressNum + '</a>';
        h += '<td><a href="ucoupon.html?uid=' + d[i].id + '" style="color: #0088cc;">' + d[i].couponNum + '</a>';
        //h += '<td><a href="urecharge.html?uid=' + d[i].id + '" style="color: #0088cc;">' + d[i].rechargeNum + '</a>';
        h += '<td><a href="javascript:;" style="color: #0088cc;"  onclick="urecharge(\'充值记录\',\'urecharge.html\', ' + d[i].id + ');">' + d[i].rechargeNum + '</a>';
        h += '<td>' + d[i].lasttime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="urecharge(\'充值记录\',\'urecharge.html\', ' + d[i].id + ');">';
        h += '充值记录';
        h += '</a>';
        h += ' <br> ';
        h += '<a href="javascript:;" onclick="lay_bank(\'提现卡号\',\'ubank.html\', ' + d[i].id + ');">';
        h += '提现卡号';
        h += '</a>';
        h += ' <br> ';
        h += '<a href="javascript:;" onclick="utixian(\'提现记录\',\'utixian.html\', ' + d[i].id + ');">';
        h += '提现记录';
        h += '</a>';
        h += ' <br/> ';
        h += '<a href="javascript:;" onclick="user_edit(\'编辑\',\'user-edit.html\', ' + d[i].id + ');" style="text-decoration:none">';
        h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' <br> ';
        h += '<a href="javascript:;" onclick="uvip_edit(\'设置等级\',\'uvip-edit.html\', ' + d[i].id + ');">';
        h += '设置等级';
        h += '</a>';
        h += ' <br/> ';
        h += '<a href="javascript:;" onclick="uorder(\'消费统计\',\'uorder.html\', ' + d[i].id + ');">';
        h += '消费统计';
        h += '</a>';
        h += ' <br/> ';
        h += '<a href="javascript:;" onclick="lay_fx(\'分销列表\',\'ufx-list.html\', ' + d[i].id + ');">';
        h += '分销列表';
        h += '</a>';
        h += ' <br/> ';
        h += '<a href="javascript:;" onclick="uorder(\'标签集合\',\'utag-list.html\', ' + d[i].id + ');">';
        h += '标签集合';
        h += '</a>';
        h += ' <br/> ';
        h += '<a href="javascript:;" onclick="del(' + d[i].id + ');">';
        h += '删除';
        h += '</a>';
        h += '<input type="hidden" id="ln' + d[i].id + '" value="' + d[i].levelName + '"/>';
        h += '<input type="hidden" id="discount' + d[i].id + '" value="' + d[i].discount + '"/>';
        h += '<input type="hidden" id="nickname' + d[i].id + '" value="' + d[i].nickname + '"/>';
        h += '<input type="hidden" id="phone' + d[i].id + '" value="' + d[i].phone + '"/>';
        h += '<input type="hidden" id="avatarUrl' + d[i].id + '" value="' + d[i].avatarUrl + '"/>';
        h += '<input type="hidden" id="is_auto' + d[i].id + '" value="' + d[i].is_auto + '"/>';
        h += '<input type="hidden" id="level_id' + d[i].id + '" value="' + d[i].level_id + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}
function del(id){

    layer.confirm('确定删除？',{
        btn: ['确定','取消'],
        yes: function(index, layero){
            $.ajax({
                type: 'post',
                url: apis.userdel.url,
                data: {id: id},
                success: function (res) {
                    console.log(res);
                    if (res.code < 0) {
                        msgPop({msg: res.msg}); return
                    }
                    layer.msg('删除成功！', {icon: 1, time: 1500});

                    getList();
                },
                error: function () {
                    console.log('错误啦！');
                }
            });
        }
    })
}

//充值记录弹窗
function urecharge(title,url,id,w,h){
    url += '?uid=' + id;
    layer_show(title,url,w,h);
}


//提现记录弹窗
function utixian(title,url,id,w,h){
    url += '?uid=' + id;
    layer_show(title,url,w,h);
}


//消费统计弹窗
function uorder(title,url,id,w,h){
    url += '?uid=' + id;
    layer_show(title,url,w,h);
}


//编辑弹窗
function user_edit(title,url,id,w,h){
    url += '?id=' + id;
    var nickname = $('#nickname' + id).val();
    var phone = $('#phone' + id).val();
    var avatarUrl = $('#avatarUrl' + id).val();
    url += '&nickname=' + escape(nickname);
    url += '&phone=' + escape(phone);
    url += '&avatarUrl=' + escape(avatarUrl);
    layer_show(title,url,w,h);
}


//卡号弹窗
function lay_bank(title,url,id,w,h){
    url += '?uid=' + id;
    layer_show(title,url,w,h);
}


//分销弹窗
function lay_fx(title,url,id,w,h){
    url += '?uid=' + id;
    layer_show(title,url,w,h);
}


//标签弹窗
function lay_tag(title,url,id,w,h){
    url += '?uid=' + id;
    layer_show(title,url,w,h);
}


//显示主图
function showImg(e){
    var h = '', i;
    h += '<img src="' + e.domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).width(e.w).height(e.h);
}


////等级弹窗
//function uvip_edit(title,url,id,w,h){
//    url += '?id=' + id;
//    var ln = $('#ln' + id).val();
//    var discount = $('#discount' + id).val();
//    var isauto = $('#is_auto' + id).val();
//    var levelId = $('#level_id' + id).val();
//    url += '&ln=' + escape(ln);
//    url += '&discount=' + discount;
//    url += '&is_auto=' + isauto;
//    url += '&level_id=' + levelId;
//    layer_show(title,url,w,300);
//}


////设置等级
//function vipModify(){
//    var formData = {
//        id: id,
//        ln: $('#title').val(),
//        discount: $('#discount').val(),
//    };
//    console.log(formData);
//    $.ajax({
//        type: apis.userVipModify.method,
//        url: apis.userVipModify.url,
//        data: formData,
//        success: function (res) {
//            console.log(res);
//            if (res.code < 0) {
//                msgPop({msg: res.msg});
//                return
//            }
//            msgPop({msg: '操作成功'});
//            setTimeout(function () {
//                top.location.reload();
//            }, 1000);
//        },
//        error: function () {
//            console.log('错误辣');
//        }
//    });
//}


//等级弹窗
function uvip_edit(title,url,id,w,h){
    url += '?id=' + id;
    var discount = $('#discount' + id).val();
    var isauto = $('#is_auto' + id).val();
    var levelId = $('#level_id' + id).val();
    url += '&discount=' + discount;
    url += '&is_auto=' + isauto;
    url += '&level_id=' + levelId;
    layer_show(title,url,w,300);
}


//等级获取
function levelSel(v){
    $.ajax({
        type: apis.userOption.method,
        url: apis.userOption.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data.level;
            var h = '', i;
            h = '';
            h += '<option value="">-选择会员等级-</option>';
            for  (i = 0; i < d.length; i ++) {
                if (d[i].type == 1) {
                    if (v == d[i].id) {
                        h += '<option value="' + d[i].id + '" selected>';
                    } else {
                        h += '<option value="' + d[i].id + '">';
                    }
                    h += d[i].title;
                    h += '</option>';
                }
            }
            $('#levelid').html(h);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//切换等级类型
function toLevel(v){
    console.log(v);
    var h = '';
    var type01Div = $('#type-1'), type02Div = $('#type-2');
    //<option value="1">SVIP【手动】</option>
    //<option value="2">黄金/铂金/钻石【手动】</option>
    //<option value="3">黄金/铂金/钻石【自动】</option>
    if (v == 1) {
        h += '<option value="0">普通会员</option>';
        h += '<option value="1" selected>黄金/铂金/钻石【手动】</option>';
        h += '<option value="2">黄金/铂金/钻石【自动】</option>';
        h += '<option value="3">SVIP【手动】</option>';
        type01Div.hide(); //折扣率
        type02Div.show(); //手动会员
    } else if (v == 2) {
        h += '<option value="0">普通会员</option>';
        h += '<option value="1">黄金/铂金/钻石【手动】</option>';
        h += '<option value="2" selected>黄金/铂金/钻石【自动】</option>';
        h += '<option value="3">SVIP【手动】</option>';
        type01Div.hide();
        type02Div.hide();
    } else if (v == 3) {
        h += '<option value="0">普通会员</option>';
        h += '<option value="1">黄金/铂金/钻石【手动】</option>';
        h += '<option value="2">黄金/铂金/钻石【自动】</option>';
        h += '<option value="3" selected>SVIP【手动】</option>';
        type01Div.show();
        type02Div.hide();
    } else {
        h += '<option value="0" selected>普通会员</option>';
        h += '<option value="1">黄金/铂金/钻石【手动】</option>';
        h += '<option value="2">黄金/铂金/钻石【自动】</option>';
        h += '<option value="3">SVIP【手动】</option>';
        type01Div.hide();
        type02Div.hide();
    }
    $('#type').html(h);
}


//设置等级
function vipModify(){
    var formData = {
        id: id,
        type: $('#type').val(),
        discount: $('#discount').val(),
        levelid: $('#levelid').val(),
    };
    console.log(formData);
    $.ajax({
        type: apis.userVipModify.method,
        url: apis.userVipModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function () {
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//修改
function modify(){
    var formData = {
        id: id,
        phone: $('#phone').val(),
        nickname: $('#nickname').val(),
        img: $('#img').val(),
    };
    if (!formData.nickname) {
        msgPop({msg: '昵称必须的'});
        return
    }
    console.log(formData);
    $.ajax({
        type: apis.userModify.method,
        url: apis.userModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function () {
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//导出，被废弃
function daochu(){
    var phone = $('#phone').val();
    var nick = $('#nick').val();
    var formData = {
        phone: phone,
        nick: nick,
    };
    console.log(formData);
    $.ajax({
        type: apis.userOut.method,
        url: apis.userOut.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            //msgPop({msg: '操作成功'});
            //setTimeout(function () {
            //    top.location.reload();
            //}, 1000);
            $('#down').html('<a href="' + res.data + '" target="_blank">下载数据</a>');
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//卡号列表
function bankList(){
    var formData = {
        uid: uid,
        start: pages.start,
        limit: pages.limit,
    };
    $.ajax({
        type: apis.userBankList.method,
        url: apis.userBankList.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            var d = res.data;
            h = '';
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].id + '</td>';
                h += '<td>' + d[i].uname + '</td>';
                h += '<td>' + d[i].realname + '</td>';
                h += '<td>' + d[i].phone + '</td>';
                h += '<td>' + d[i].bank_name + '</td>';
                h += '<td>' + d[i].card_no + '</td>';
                h += '<td>' + d[i].default + '</td>';
                h += '<td>' + d[i].addtime + '</td>';
                h += '<td class="f-14">';
                if (d[i].is_default == 0) {
                    h += '<a href="javascript:;" onclick="toDefault(' + d[i].id + ');">';
                    h += '设为默认';
                    h += '</a>';
                } else {
                    h += '-';
                }
                h += '</td>';
                h += '</tr>';
            }
            tbody.html(h);
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//卡号默认设置
function toDefault(v){
    var msg = '确认要 <b style="color: green; font-size: 20px;">默认</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.userBankDefaultModify.method,
            url: apis.userBankDefaultModify.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('操作成功!',{icon: 1,time: 50});
                msgPop({msg: '操作成功'});
                setTimeout(function(){
                    location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//提现审核列表
function txAuthList(){
    $.ajax({
        type: apis.userTxAuthList.method,
        url: apis.userTxAuthList.url,
        data: {},
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '';
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            var d = res.data;
            h = '';
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].id + '</td>';
                h += '<td>' + d[i].uname + '</td>';
                h += '<td>' + d[i].phone + '</td>';
                h += '<td>' + d[i].bank_info + '</td>';
                h += '<td>' + d[i].amount + '</td>';
                h += '<td>' + d[i].addtime + '</td>';
                h += '<td class="f-14">';
                if (d[i].status == 1) {
                    h += '<a href="javascript:;" onclick="auth_ok(' + d[i].id + ');">';
                    h += '通过';
                    h += '</a>';
                    h += ' &nbsp; ';
                    h += '<a href="javascript:;" onclick="auth_no(' + d[i].id + ');">';
                    h += '拒绝';
                    h += '</a>';
                } else {
                    h += '-';
                }
                h += '</td>';
                h += '</tr>';
            }
            tbody.html(h);
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误辣');
        }
    });
}


//提现审核通过
function auth_ok(v){
    var msg = '确认要 <b style="color: green; font-size: 20px;">通过</b> 吗？';
    layer.confirm(msg,function(index){
        authApi({id: v, auth: 2});
    });
}
//提现审核拒绝
function auth_no(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">拒绝</b> 吗？';
    layer.confirm(msg,function(index){
        authApi({id: v, auth: 1});
    });
}
//提现审核API
function authApi(e){
    $.ajax({
        type: apis.userTxAuthModify.method,
        url: apis.userTxAuthModify.url,
        data: { id: e.id, auth: e.auth },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            layer.msg('操作成功!',{icon: 1,time: 50});
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//分销列表
function fxList(uid){
    $.ajax({
        type: apis.userFxList.method,
        url: apis.userFxList.url,
        data: { uid: uid },
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            var d = res.data;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].uname + '</td>';
                h += '<td>' + d[i].amount + '</td>';
                h += '<td>' + d[i].addtime + '</td>';
                h += '</tr>';
            }
            console.log(h);
            tbody.html(h);
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//标签列表
function tagList(uid){
    $.ajax({
        type: apis.userTagList.method,
        url: apis.userTagList.url,
        data: { uid: uid },
        success: function (res) {
            console.log(res);
            var h = '', i, tbody = $('tbody');
            if (res.code < 0) {
                h += '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            var d = res.data;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].title + '</td>';
                h += '<td>' + d[i].addtime + '</td>';
                h += '<td class="f-14">';
                h += '<a href="javascript:;" style="color: red;" onclick="tag_del(' + d[i].id + ');">';
                h += '删除';
                h += '</a>';
                h += '</td>';
                h += '</tr>';
            }
            console.log(h);
            tbody.html(h);
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//标签添加
function tagAdd(){
    var formData = {
        uid: uid, title: $('#title').val(),
    };
    if (!formData.title) {
        msgPop({msg: '没有标签名'});
        return
    }
    $.ajax({
        type: apis.userTagAdd.method,
        url: apis.userTagAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            layer.msg('操作成功!',{icon: 1,time: 50});
            setTimeout(function(){
                //top.location.reload();
                location.reload();
            }, 500);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//标签删除
function tag_del(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">拒绝</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.userTagDel.method,
            url: apis.userTagDel.url,
            data: { id: v },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('操作成功!',{icon: 1,time: 50});
                setTimeout(function(){
                    location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}